package com.iweb.dao.impl;

import com.iweb.dao.BaseDao;
import com.iweb.dao.ProductDao;
import com.iweb.entity.Product;
import com.iweb.util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jay
 * @date 2021/9/13
 * @description
 */
public class ProductDaoImp extends BaseDao implements ProductDao {
    @Override
    public void insert(Product product) throws Exception {
        String sql = "insert into `littlemall_product` values(null, ?,?,?,?,?,?,?,?,?)";
        Object[] params = {
                product.getName(),
                product.getDescription(),
                product.getPrice(),
                product.getStock(),
                product.getCategoryLevel1Id(),
                product.getCategoryLevel2Id(),
                product.getCategoryLevel3Id(),
                product.getFileName(),
                product.getIsDelete()
        };
        this.executeUpdate(sql, params);
    }

    @Override
    public List<Product> selectListByTypeOne(Integer typeOneId, int rows) throws Exception {
        List<Product> productList = new ArrayList<>();
        // 编写sql
        String sql = "select id, name, price, fileName from littlemall_product where categoryLevel1Id=? limit 0, ?";
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBHelper.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = this.executeQuery(connection, preparedStatement, typeOneId, rows);

            Product product = null;
            // 解析数据
            while (resultSet.next()) {
                product = parseResultSet(resultSet);
                productList.add(product);
            }
            return productList;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBHelper.close(resultSet, preparedStatement, connection);
        }
    }

    @Override
    public Product selectById(Integer id) throws Exception {
        // 编写sql
        String sql = "select * from littlemall_product where id=?";
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBHelper.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = this.executeQuery(connection, preparedStatement, id);

            Product product = null;
            // 解析数据
            if (resultSet.next()) {
                product = parseResultSet(resultSet);
            }
            return product;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBHelper.close(resultSet, preparedStatement, connection);
        }
    }

    /**
     * 解析结果
     *
     * @param resultSet
     * @return
     * @throws SQLException
     */
    private Product parseResultSet(ResultSet resultSet) throws SQLException {
        Product product = new Product();
        product.setId(resultSet.getInt("id"));
        product.setName(resultSet.getString("name"));
        product.setPrice(resultSet.getDouble("price"));
        product.setFileName(resultSet.getString("fileName"));
        return product;
    }
}
